/**
  * This file is part of Medusa.
  *
  * Authors: Uriel Fanelli <uriel.fanelli@gmail.com>
  *          Perthect <perthect@gmail.com>
  *          Marco Bizzarri <Marco.Bizzarri@gmail.com>
  *
  *
  * Medusa is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.

  * Medusa is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.

  * You should have received a copy of the GNU General Public License
  * along with Medusa; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  */


#include "proxy_dialog.h"
#include "proxy_settings.h"



dialogProxyConfig::dialogProxyConfig()
{
    //creiamoci un oggetto proxyconfig
    proxyconf_interna = new handleProxyConfig;

   proxydialog=new QWidget;
   // settiamo il mainLayout
   mainLayout = new QGridLayout;

    //sistemiamo la riga host
    host_layout = new QHBoxLayout;
    host_label = new QLabel(tr("Host:"));
    host_line = new QLineEdit;
    host_line->setText(proxyconf_interna->proxy_host);
    host_label->setBuddy(host_line);
    host_layout->addWidget(host_label);
    host_layout->addWidget(host_line);
    mainLayout->addLayout(host_layout, 0, 0);

   //sistemiamo la riga port
    port_layout = new QHBoxLayout;
    port_label = new QLabel(tr("Port:"));
    port_line= new QLineEdit;
    port_line->setText(proxyconf_interna->proxy_port);
    port_label->setBuddy(port_line);
    port_layout->addWidget(port_label);
    port_layout->addWidget(port_line);
    mainLayout->addLayout(port_layout, 1, 0);

 //sistemiamo la riga username
    user_layout = new QHBoxLayout;
    user_label = new QLabel(tr("User:"));
    user_line= new QLineEdit;
    user_line->setText(proxyconf_interna->proxy_user);
    user_label->setBuddy(user_line);
    user_layout->addWidget(user_label);
    user_layout->addWidget(user_line);
    mainLayout->addLayout(user_layout, 2, 0);

     //sistemiamo la riga password
    pass_layout = new QHBoxLayout;
    pass_label = new QLabel(tr("Pass:"));
    pass_line= new QLineEdit;
    pass_line->setEchoMode(QLineEdit::Password);
    pass_line->setText(proxyconf_interna->proxy_pass);
    pass_label->setBuddy(pass_line);
    pass_layout->addWidget(pass_label);
    pass_layout->addWidget(pass_line);
    mainLayout->addLayout(pass_layout, 3, 0);

    // mettiamo le checkbox
    type_layout = new QHBoxLayout;
    is_http_check = new QCheckBox(tr("Http"));
    is_socks_check = new QCheckBox(tr("Socks 5"));
    is_http_check->setChecked(false);
    is_socks_check->setChecked(false);

    type_layout->addWidget(is_http_check);
    type_layout->addWidget(is_socks_check);
    mainLayout->addLayout(type_layout, 4, 0);

    if (proxyconf_interna->proxy_type == "http")
    {
        is_http_check->setChecked(true);
        is_socks_check->setChecked(false);
    }

    if (proxyconf_interna->proxy_type == "socks")
    {
        is_http_check->setChecked(false);
         is_socks_check->setChecked(false);
    }



 // sistemiamo i bottoni
    bottoni_layout = new QHBoxLayout;
    ok = new QPushButton(tr("&OK"));
    ok->setIcon(QIcon(":/resources/icons/sent.png"));
    cancel = new QPushButton(tr("&Cancel"));
    bottoni_layout->addWidget(ok);
    bottoni_layout->addWidget(cancel);
    mainLayout->addLayout(bottoni_layout, 5, 0);


// la finestra e' qui
    proxydialog->setLayout(mainLayout);
    proxydialog->setWindowTitle("Proxy");
    proxydialog->setWindowIcon(QIcon(":/resources/icons/proxy.png"));
    proxydialog->show();

connect(ok,SIGNAL(clicked()),this,SLOT(ok_proxy() ));
connect(cancel,SIGNAL(clicked()),this,SLOT(do_nothing() ));

}


void dialogProxyConfig::do_nothing()
{

proxydialog->hide();
delete this;
}
void dialogProxyConfig::ok_proxy()
{
proxyconf_interna->proxy_host = host_line->text();
proxyconf_interna->proxy_port = port_line->text();
proxyconf_interna->proxy_user = user_line->text();
proxyconf_interna->proxy_pass = pass_line->text();



if (is_http_check->isChecked() != is_socks_check->isChecked())
{


    if (is_http_check->isChecked() )
    {
        proxyconf_interna->proxy_type = "http";
    }

    else
        {
        proxyconf_interna->proxy_type = "socks";
        }

}

if (is_http_check->isChecked() == is_socks_check->isChecked())

     {
        proxyconf_interna->proxy_type = "";
      }


proxyconf_interna->deployProxy();
proxyconf_interna->salvaProxyConfig();
proxydialog->hide();
delete this;

}
